package 链表;

/**
 * @author aodre , QQ : 480029069
 * @date 2023/2/15 20:45
 */
public class leetcode25每k个一组反转链表 {




    /*
     理解 了 ,去写,是非常简单的
     多去看 灵神 关于链表的讲解
     */

    public ListNode solve(ListNode head, int k) {
        ListNode sentineel = new ListNode(-1), p0 = sentineel, cur = head;
        sentineel.next = head;
        int num = 0;
        while (cur != null) {
            num++;
            cur = cur.next;
        }

        ListNode pre = null, next = null, temp = null;
        cur = head;
        while (num >= k) {
            num -= k;
            for (int i = 0; i < k; i++) {
                temp = cur.next;
                cur.next = pre;
                pre = cur;
                cur = temp;
            }
            next = p0.next;
            p0.next.next =cur;
            p0.next = pre;
            p0 = next;
        }
        return sentineel.next;
    }



}
